package xyc.szk.web.servlet.request;
/*
 * @Description:
 * @Author: 山志凯
 * @Date: 2021-03-31
 * @Time: 08:18
 */

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/req01") //mapping--映射为一个地址，浏览器就可以通过地址来访问这个类的doGet和doPost方法
public class RequestDispather01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决中文乱码，这句话要放在最前面
        resp.setContentType("text/html;charset=utf-8");
        //获取 请求转发器
        RequestDispatcher dispatcher = req.getRequestDispatcher("/req02");
        //转发器请求
        dispatcher.forward(req,resp);
        System.out.println("req01:" + req + "\nresp01" + resp);
        //获取响应器，用来向浏览器发送信息，下面这个响应不会生效，要到req02响应
        PrintWriter out = resp.getWriter();
        out.println("req01的响应信息");

        //将{username:tom}对象存在request的域空间中，它只能在这次请求中生效
        req.setAttribute("username","shanzhikai");


        //请求包含
        dispatcher.include(req,resp); //了解

        //请求转发：它是同一个请求，它是一次请求一次响应
        dispatcher.forward(req,resp); //掌握
    }
}
